package org.example.classtopia.Utils;

import org.example.classtopia.Model.Entity.Curriculum;
import org.example.classtopia.Model.Entity.Course;

import java.util.Random;

public class CurriculumToCourseConverter {
    private static final String[] COLORS = {
            "#6a9eff",
            "#a3cef1",
            "#f7cac9",
            "#ffb482",
            "#ffe156",
            "#88d8b0",
            "#d4a5a5",
            "#92a8d1"
    };

    private static final Random RANDOM = new Random();

    public static String getRandomColor() {
        int index = RANDOM.nextInt(COLORS.length);
        return COLORS[index];
    }
    /**
     * 将Curriculum实体转换为Course实体。
     * @param curriculum 课程实体
     * @param semester 学期开始日期字符串，格式如"2025-02-17"，必须传入且不能为空
     * @return 转换后的Course对象，保证semester字段非空
     */
    public static Course convert(Curriculum curriculum, String semester) {
        Course course = new Course();

        course.setUserId(curriculum.getUserId());
        course.setEventType("course");
        course.setName(curriculum.getCourseName());
        course.setTeacher(curriculum.getTeacher());
        course.setLocation(curriculum.getClassroom());
        course.setDayOfWeek(curriculum.getDay());
        course.setStartWeek(curriculum.getFromWeek());
        course.setEndWeek(curriculum.getEndWeek());
        course.setSemester(semester); // **关键：必须赋值且非空**

        course.setStartTime(classToTime(curriculum.getFromClass()));

        // endTime需要加45分钟
        String endClassStart = classToTime(curriculum.getEndClass());
        String endTimeWithAdded = add45Minutes(endClassStart);
        course.setEndTime(endTimeWithAdded);

        course.setStatus(1);       // 设置状态为正常
        course.setIsReminded(0);  // 默认为未提醒

        // 其他字段可根据需要补充，比如颜色、描述等
        // 使用随机温和颜色
        course.setColor(getRandomColor());
        return course;
    }

    public static String add45Minutes(String timeStr) {
        if (timeStr == null || timeStr.equals("00:00")) {
            return "00:00";
        }
        try {
            java.time.LocalTime time = java.time.LocalTime.parse(timeStr);
            java.time.LocalTime newTime = time.plusMinutes(45);
            return newTime.toString(); // 默认格式HH:mm:ss，如果要HH:mm可用格式化
        } catch (Exception e) {
            return "00:00"; // 解析失败返回默认时间
        }
    }

    /**
     * 简单示例：将节次数字转换成时间字符串，需根据实际课程时间表调整
     */
    public static String classToTime(Integer classNumber) {
        if (classNumber == null) {
            return "00:00";
        }
        switch (classNumber) {
            case 1: return "08:00";
            case 2: return "08:50";
            case 3: return "09:50";
            case 4: return "10:40";
            case 5: return "11:30";
            case 6: return "14:05";
            case 7: return "14:55";
            case 8: return "15:45";
            case 9: return "16:40";
            case 10: return "17:30";
            case 11: return "18:30";
            case 12: return "19:20";
            case 13: return "20:10";
            default: return "00:00";
        }
    }
}

